import { UITransform, v3 } from "cc";
import { app } from "db://app-game/core/app";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { ECSEntity } from "db://app-game/libs/ecs/ECSEntity";
import { CameraModelComp } from "../model/CameraModelComp";

/** 摄像机跟随系统 */
@ecs.register("CameraFollow")
export class CameraFollowSystem
  extends ecs.ComblockSystem
  implements ecs.ISystemUpdate
{
  filter(): ecs.IMatcher {
    return ecs.allOf(CameraModelComp);
  }
  update(entity: ECSEntity, dt: number): void {
    let comp = entity.get(CameraModelComp);
    if (comp.target) {
      let wPos = comp.target
        .getComponent(UITransform)
        .convertToWorldSpaceAR(v3(0, 0, 0));

      let nPos = app.gui.game
        .getComponent(UITransform)
        .convertToNodeSpaceAR(wPos);
      comp.cameraNode.position = nPos;
    }
  }
}
